package com.tcs.netty.channel.handler;

import com.tcs.model.datastandardization.RequestStanBean;
import com.tcs.model.datastandardization.ResponseStanBean;
import com.tcs.service.IDataHandleService;
import com.tcs.util.spring.SpringConfigTool;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;

public class ServerChannelhandler extends ChannelInboundHandlerAdapter {

    private final Logger logger = LoggerFactory.getLogger(ServerChannelhandler.class);

    private IDataHandleService iDataHandleService;

    public ServerChannelhandler() {
        super();
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        logger.info("channel注册到一个EventLoop。");
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        logger.info("channel已创建但未注册到一个EventLoop。");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        logger.info("channel变为活跃状态（连接到了远程主机），现在可以接收和发送数据了");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        logger.info("channel处于非活跃状态，没有连接到远程主机");
    }

    @Override
    protected void ensureNotSharable() {
        super.ensureNotSharable();
    }

    @Override
    public boolean isSharable() {
        return super.isSharable();
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        logger.info("添加 handlerADDed");
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        logger.info("删除 handlerADDed");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            logger.info(" channelRead ");
            iDataHandleService = (IDataHandleService) SpringConfigTool.getBean("dataHandleService");
        } catch (BeansException e) {
            logger.error("", e);
        } finally {
            logger.info("SpringConfigTool : {} , iDataHandleService: {}", SpringConfigTool.getApplicationContext() , iDataHandleService);
        }
        try {
            RequestStanBean requestStanBean = (RequestStanBean) msg;
//            Action action = staticPack.get(requestStanBean.getCommod());
//            ResponseStanBean responseStanBean = (ResponseStanBean) action.getMethod().invoke(action.getBean(), ctx, msg);
            ResponseStanBean responseStanBean = iDataHandleService.handleData(ctx, requestStanBean);
            logger.info(" requestStanBean: {} , responseStanBean: {}", requestStanBean, responseStanBean);
            ctx.channel().writeAndFlush(responseStanBean);
        } catch (Exception e) {
            logger.error("", e);
        }

    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        logger.info("最后一次读取  ");
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        super.channelWritabilityChanged(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        logger.error("", cause);
    }
}
